package com.bank;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.config.*;

import javax.sql.DataSource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;

import static java.awt.Color.black;
import static java.awt.Color.yellow;

public class B extends JFrame implements ActionListener {
    private JLabel label1, label2,label3;
    private JTextField userName,money,yuan;//
    private JPasswordField password;//
    private JButton button1, button2,button3,button4,button5,button31,button41,button51;
    JButton jButton = new JButton("OK");
    private JFrame jFrame11=new JFrame("");//其他账户界面
    private JFrame jFrame111=new JFrame("");
    private JFrame jFrame2=new JFrame("注册界面");
    Connection connection = null;
    JLabel jLabel1 = new JLabel("姓名");
    JLabel jLabel2 = new JLabel("ID");
    JLabel jLabel3 = new JLabel("账户种类");
    JLabel jLabel4 = new JLabel("密码");
    JLabel jLabel5 = new JLabel("首充金额");
    JLabel jLabel6 = new JLabel("利率/手续");
    JTextField jTextField1=new JTextField(25);
    JTextField jTextField2=new JTextField(25);
    JTextField jTextField3=new JTextField(25);
    JTextField jTextField4=new JTextField(25);
    JTextField jTextField5=new JTextField(25);
    String[] kinds=new String[3];
    ResultSet rs;
    JComboBox jComboBox ;
    double balance;int id;
    String record;
    public B() {
        getContentPane().setLayout(new BorderLayout(0, 0));
        JPanel panel = new ImagePanel();
        getContentPane().add(panel);
        //
        panel.setLayout(null);
        //
        label1 = new JLabel(" ID ");
        label1.setFont(new Font("宋体", Font.BOLD, 26));
        label1.setForeground(yellow);
        label1.setBounds(475, 350, 100, 50);
        label2 = new JLabel("密码");
        label2.setFont(new Font("宋体", Font.BOLD, 26));
        label2.setForeground(yellow);
        label2.setBounds(475, 430, 100, 50);
        label3=new JLabel("Hzy");
        label3.setBounds(800,180,50,30);
        // 添加两个输入框，用于输入用户名和密码。用户名用JTextFied，密码用JPasswordField
        userName = new JTextField(10);
        userName.setBounds(575, 350, 200, 50);
        userName.setFont(new Font("宋体", Font.BOLD, 20));
        userName.setColumns(15);

        password = new JPasswordField(10);
        password.setBounds(575, 430, 200, 50);
        password.setFont(new Font("宋体", Font.BOLD, 30));
        password.setColumns(15);

        // 添加两个按钮，用于显示登录和注销功能！！
        button1 = new JButton("登陆");
        button1.setFont(new Font("宋体", Font.BOLD, 26));
        button1.setForeground(black);
        button1.setBounds(555, 530, 100, 50);
        button1.addActionListener(this);


        button2 = new JButton("注册");
        button2.setFont(new Font("宋体", Font.BOLD, 26));
        button2.setForeground(black);
        button2.setBounds(700, 530, 100, 50);
        button2.addActionListener(this);

        // 添加组件
        panel.add(label1);
        panel.add(userName);
        panel.add(label2);
        panel.add(label3);
        panel.add(password);
        panel.add(button1);
        panel.add(button2);
    }
    public static void main(String args[]) throws SQLException, ClassNotFoundException {
        B frame = new B();
        frame.setSize(900, 700);// 窗体大下
        frame.setLocation(100, 100);// 窗口初始位置
        frame.setResizable(false);// 不可更改窗体大小
        frame.setTitle("银行账户管理系统");
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);// 默认关闭方式
        frame.setVisible(true);// 窗口可见
//        ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
//        DataSource d = (DataSource) ac.getBean("dataSource");
//        Connection c = d.getConnection();
        AnnotationConfigApplicationContext actx=new AnnotationConfigApplicationContext(Config.class);
        DataSource ds=(DataSource) actx.getBean("datasource");
        sun.rmi.transport.Connection conn=(sun.rmi.transport.Connection) ds.getConnection();

//        String user="root";
//        String password="hsp";
//        String url="jdbc:mysql://127.0.0.1:3306/db01";
//        Properties properties = new Properties();
//        properties.setProperty("user","root");
//        properties.setProperty("password","hsp");
//        Class.forName("com.mysql.jdbc.Driver");
//        frame.connection= DriverManager.getConnection(url,user,password);

        //Admin.menuItem1.addActionListener(this);
        //String sql="create table accounts(name varchar(20),id int, kind varchar(10),password varchar(100),balance double,record text,others double)";
        //Statement statement = connection.createStatement();
        //statement.executeUpdate(sql);
        //statement.close();
        //connection.close();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==button1){
            String s1=userName.getText();
            String s2=new String(password.getPassword());
            int key=0;
            String sql="select * from accounts";
            String kind=new String("");String password=new String("");
            String name=new String();
            try {
                Statement stmt=connection.createStatement();
                rs=stmt.executeQuery(sql);
                while (rs.next()){
                    name=rs.getString("name");
                    id=rs.getInt("id");
                    kind=rs.getString("kind");
                    password=rs.getString("password");
                    balance=rs.getDouble("balance");
                    record=rs.getString("record");
                    //System.out.println(id+" "+password);
                    if(Integer.parseInt(s1)==id&&s2.equals(password)){
                        key=1;
                        break;
                    }
                }

            } catch (SQLException throwables) {
                JOptionPane.showMessageDialog(null, "操作失败", "警告",JOptionPane.WARNING_MESSAGE);
            }
            if(s1.equals("1")&&s2.equals("2")){
                try {
                    Admin admin = new Admin(rs);
                } catch (SQLException throwables) {
                    JOptionPane.showMessageDialog(null, "数据非法！！", "警告",JOptionPane.WARNING_MESSAGE);
                }
            }
            else if(key==1){
                if(kind.equals("信用")){
                    JPanel jPanel=new JPanel(new FlowLayout());
                    yuan=new JTextField(25);
                    button31=new JButton("贷款");
                    button41=new JButton("还款");
                    button51=new JButton("查看余额");
                    jPanel.add(yuan);
                    jPanel.add(button31);
                    jPanel.add(button41);
                    jPanel.add(button51);
                    jFrame111.add(jPanel);
                    jFrame111.setSize(330,100);
                    jFrame111.setTitle(name);
                    jFrame111.setLocation(400,350);
                    jFrame111.setResizable(false);
                    jFrame111.setVisible(true);
                    button31.addActionListener(this);
                    button41.addActionListener(this);
                    button51.addActionListener(this);
                }
                else{
                    JPanel jPanel=new JPanel(new FlowLayout());
                    money=new JTextField(25);
                    button3=new JButton("存款");
                    button4=new JButton("取钱");
                    button5=new JButton("查看余额");
                    jPanel.add(money);
                    jPanel.add(button3);
                    jPanel.add(button4);
                    jPanel.add(button5);
                    jFrame11.add(jPanel);
                    jFrame11.setSize(330,100);
                    jFrame11.setTitle(name);
                    jFrame11.setLocation(400,350);
                    jFrame11.setResizable(false);
                    jFrame11.setVisible(true);
                    button3.addActionListener(this);
                    button4.addActionListener(this);
                    button5.addActionListener(this);
                }
            }
            else{
                JOptionPane.showMessageDialog(null, "用户不存", "警告",JOptionPane.WARNING_MESSAGE);
            }
        }
        if(e.getSource()==button2){
            kinds[0]="储蓄账户";kinds[1]="支票账户";kinds[2]="信用卡账";
            jComboBox=new JComboBox(kinds);
            jButton.addActionListener(this);
            String s1=userName.getText();
            String s2=new String(password.getPassword());
            jFrame2.setSize(440,480);
            if(s1.equals("1")&&s2.equals("2")){
                JLabel[] labelBox=new JLabel[27];
                for(int i=0;i<27;i++){
                    labelBox[i]=new JLabel("");
                }
                JPanel jp=new JPanel(new GridLayout(8,5,3,10));
                int k=0;
                for(int i=0;i<8;i++){
                    for (int j = 0; j < 5; j++) {
                        if(i==0&&j==1){
                            jp.add(jLabel1);
                        }
                        else if(i==0&&j==3){
                            jp.add(jTextField1);
                        }
                        else if(i==1&&j==1){
                            jp.add(jLabel2);
                        }
                        else if(i==1&&j==3){
                            jp.add(jTextField2);
                        }
                        else if(i==2&&j==1){
                            jp.add(jLabel3);
                        }
                        else if(i==2&&j==3){
                            jp.add(jComboBox);
                        }
                        else if(i==3&&j==1){
                            jp.add(jLabel4);
                        }
                        else if(i==3&&j==3){
                            jp.add(jTextField3);
                        }
                        else if(i==4&&j==1){
                            jp.add(jLabel5);
                        }
                        else if(i==4&&j==3){
                            jp.add(jTextField4);
                        }
                        else if(i==5&&j==1){
                            jp.add(jLabel6);
                        }
                        else if(i==5&&j==3){
                            jp.add(jTextField5);
                        }
                        else if(i==7&&j==2){
                            jp.add(jButton);
                        }
                        else {
                            jp.add(labelBox[k]);
                            k++;
                        }
                    }
                }
                jFrame2.add(jp);
                jFrame2.setLocation(200,300);
                jFrame2.setVisible(true);
            }
         else{
                JOptionPane.showMessageDialog(null, "无权限或账号密码不匹配！！！", "警告",JOptionPane.WARNING_MESSAGE);
            }
        }
        if(e.getSource()==button3){
            //String s=jFrame11.getTitle();
            double x=balance+Integer.parseInt(money.getText());
            //String sql="update accounts set balance="+x+"where id="+id;
            Calendar calendar= Calendar.getInstance();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            String s=dateFormat.format(calendar.getTime());
            String a=new String(record+"存款"+Integer.parseInt(money.getText())+" "+s);
            String sql1="update accounts set balance="+x+"where id="+id;
            //System.out.println(sql1);
            try {
                Statement stmt=connection.createStatement();
                stmt.executeUpdate(sql1);
            } catch (SQLException throwables) {
                JOptionPane.showMessageDialog(null, "数据非法！！", "警告",JOptionPane.WARNING_MESSAGE);
            }
            String sql="update accounts set record='"+a+"'where id="+id;
            //System.out.println(sql);
            try {
                Statement stmt=connection.createStatement();
                stmt.executeUpdate(sql);
            } catch (SQLException throwables) {
                JOptionPane.showMessageDialog(null, "数据非法！！！", "警告",JOptionPane.WARNING_MESSAGE);
            }
            JOptionPane.showMessageDialog(null, "操作成功");
        }
        if(e.getSource()==button4){
            double y=balance-Integer.parseInt(money.getText());
            if(y<0){
                JOptionPane.showMessageDialog(null, "数据非法！！！", "警告",JOptionPane.WARNING_MESSAGE);
            }
            else{
                double x=balance-Integer.parseInt(money.getText());
            //String sql="update accounts set balance="+x+"where id="+id;
                Calendar calendar= Calendar.getInstance();
                SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
                String s=dateFormat.format(calendar.getTime());
                String a=new String(record+"取钱"+Integer.parseInt(money.getText())+" "+s);
                String sql1="update accounts set balance="+x+"where id="+id;
            //System.out.println(sql1);
                try {
                    Statement stmt=connection.createStatement();
                    stmt.executeUpdate(sql1);
                } catch (SQLException throwables) {
                    JOptionPane.showMessageDialog(null, "数据非法！！！", "警告",JOptionPane.WARNING_MESSAGE);
                }
                String sql="update accounts set record='"+a+"'where id="+id;
            //System.out.println(sql);
                try {
                    Statement stmt=connection.createStatement();
                    stmt.executeUpdate(sql);
                } catch (SQLException throwables) {
                    JOptionPane.showMessageDialog(null, "数据非法！！！", "警告",JOptionPane.WARNING_MESSAGE);
                }
                JOptionPane.showMessageDialog(null, "操作成功");}
        }
        if(e.getSource()==button5){
            JOptionPane.showMessageDialog(null, "您的余额是"+balance);
        }
        if(e.getSource()==jButton){
            //String sql="create table accounts(name varchar(20),id int, kind varchar(10),password varchar(100),balance double,record text,others double)";
            try{
                String sql="insert into accounts values('";
                sql+=jTextField1.getText();sql+="',";sql+=jTextField2.getText();sql+=",'";
                sql+=jComboBox.getSelectedItem();sql+="','";sql+=jTextField3.getText();sql+="',";
                sql+=jTextField4.getText();sql+=",'";

                Calendar calendar= Calendar.getInstance();
                SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
                String s=dateFormat.format(calendar.getTime());
                sql+="充值"+jTextField4.getText()+"(开户)"+s+"，',";
                sql+=jTextField5.getText();sql+=")";
                Statement statement = connection.createStatement();
                //System.out.println(sql);
                int rows=statement.executeUpdate(sql);
                if(rows==0){
                    JOptionPane.showMessageDialog(null, "添加失败！", "警告",JOptionPane.WARNING_MESSAGE);
                }
                else{
                    JOptionPane.showMessageDialog(null, "添加成功！");
                }
                statement.close();
                connection.close();
            } catch (SQLException h){
                JOptionPane.showMessageDialog(null, "操作失败！", "警告",JOptionPane.WARNING_MESSAGE);
            }
        }
        if(e.getSource()==button41){
            //String s=jFrame11.getTitle();
            double x=balance+Integer.parseInt(yuan.getText());
            //String sql="update accounts set balance="+x+"where id="+id;
            Calendar calendar= Calendar.getInstance();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            String s=dateFormat.format(calendar.getTime());
            String a=new String(record+"还款"+Integer.parseInt(yuan.getText())+" "+s);
            String sql1="update accounts set balance="+x+"where id="+id;
            //System.out.println(sql1);
            try {
                Statement stmt=connection.createStatement();
                stmt.executeUpdate(sql1);
            } catch (SQLException throwables) {
                JOptionPane.showMessageDialog(null, "数据非法！！！", "警告",JOptionPane.WARNING_MESSAGE);
            }
            String sql="update accounts set record='"+a+"'where id="+id;
            //System.out.println(sql);
            try {
                Statement stmt=connection.createStatement();
                stmt.executeUpdate(sql);
            } catch (SQLException throwables) {
                JOptionPane.showMessageDialog(null, "数据非法！！！", "警告",JOptionPane.WARNING_MESSAGE);
            }
            JOptionPane.showMessageDialog(null, "操作成功");
        }
        if(e.getSource()==button31){
                double x=balance-Integer.parseInt(yuan.getText());
                //String sql="update accounts set balance="+x+"where id="+id;
                Calendar calendar= Calendar.getInstance();
                SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
                String s=dateFormat.format(calendar.getTime());
                String a=new String(record+"贷款"+Integer.parseInt(yuan.getText())+" "+s);
                String sql1="update accounts set balance="+x+"where id="+id;
                //System.out.println(sql1);
                try {
                    Statement stmt=connection.createStatement();
                    stmt.executeUpdate(sql1);
                } catch (SQLException throwables) {
                    JOptionPane.showMessageDialog(null, "数据非法！！！", "警告",JOptionPane.WARNING_MESSAGE);
                }
                String sql="update accounts set record='"+a+"'where id="+id;
                //System.out.println(sql);
                try {
                    Statement stmt=connection.createStatement();
                    stmt.executeUpdate(sql);
                } catch (SQLException throwables) {
                    JOptionPane.showMessageDialog(null, "数据非法！！！", "警告",JOptionPane.WARNING_MESSAGE);
                }
                JOptionPane.showMessageDialog(null, "操作成功");}
        if(e.getSource()==button51){
            JOptionPane.showMessageDialog(null, "您的余额是"+balance);
        }
    }

    class ImagePanel extends JPanel {
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            ImageIcon icon = new ImageIcon("src/image/Ship.jpg");
            g.drawImage(icon.getImage(), 0, 0, null);
        }
    }



    }



